const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");

//根据票据号查询路由
router.post('/common/getRouteByBillNo', async ctx => {
    let body = ctx.request.body
    let user = ctx.user
    let routeName = body.routeName;
    let value = body.value;
    let tenantId = user.tenantId;
    let menu = {};

// 现金银行报表中的字段查看详情,|往来对账单中字段查看详情
    if (routeName === 'reportacc' || routeName === 'customerbills') {
        menu.id = 'bandc';
        menu.routeId = 'bandc';
        menu.name = '明细数据';
        return ctx.suc('', menu);
    }
    if (routeName === 's-ins') {
        if (value.toString().indexOf('XS') === -1) {
            menu.id = 'bandc';
            menu.routeId = 'bandc';
            menu.name = '明细数据';
            return ctx.suc('', menu);
        }
    }
    if (routeName === 'profit') {
        menu.id = 'cardDetail';
        menu.routeId = 'cardDetail';
        menu.name = '利润明细';
        return ctx.suc('', menu);
    }
    if (routeName === 'zcfzb') {
        menu.id = 'assetDetail';
        menu.routeId = 'assetDetail';
        menu.name = '负债表明细';
        return ctx.suc('', menu);
    }
    if (routeName === 'ch_xs') {
        menu.id = 'charts';
        menu.routeId = 'charts';
        menu.name = '销售明细图表';
        return ctx.suc('', menu);
    }
    if (routeName === 'ch_yj') {
        menu.id = 'charts';
        menu.routeId = 'charts';
        menu.name = '业绩图表';
        return ctx.suc('', menu);
    }
    if (routeName === 'kcye') {
        menu.id = 'gdetail';
        menu.routeId = 'gdetail';
        menu.name = '库存明细';
        return ctx.suc('', menu);
    }
    if(value.indexOf("PZ") === 0) {
        // 凭证
        let menuModel = await db.models['menuV5'].findOne({
            where: {
                routeId: 'card'
            }
        })
        menu.id = menuModel.id;
        menu.routeId = menuModel.routeId;
        menu.name = menuModel.name;
        return ctx.suc('', menu);
    }
    // 除凭证外的其它票据
    let sql = `select top 1 id,routeId,name from menu_v5 where isDelete=0 and name=(select case transTypeName  when N'应收' then N'应收款' when N'应付' then N'应付款' else transTypeName end
    from invoice where billNo='${value}' and tenantId=${tenantId})
    `
    let menuModel = await db.query(sql, {type: QueryTypes.SELECT})
    if (menuModel.length > 0) {
        menu.id = menuModel[0].id;
        menu.routeId = menuModel[0].routeId;
        menu.name = menuModel[0].name;
        return ctx.suc('', menu);
    }
})




module.exports = router;